package org.arabidopsis.ahocorasick;


class DenseEdgeList
        implements EdgeList {
    private final State[] array = new State[256];

    public DenseEdgeList() {
        for (int i = 0; i < this.array.length; i++) {
            this.array[i] = null;
        }
    }

    public static DenseEdgeList fromSparse(SparseEdgeList list) {
        byte[] keys = list.keys();
        DenseEdgeList newInstance = new DenseEdgeList();
        for (int i = 0; i < keys.length; i++) {
            newInstance.put(keys[i], list.get(keys[i]));
        }
        return newInstance;
    }

    public State get(byte b) {
        return this.array[b & 0xFF];
    }

    public void put(byte b, State s) {
        this.array[b & 0xFF] = s;
    }

    public byte[] keys() {
        int length = 0;
        for (int i = 0; i < this.array.length; i++) {
            if (this.array[i] != null)
                length++;
        }
        byte[] result = new byte[length];
        int j = 0;
        for (int k = 0; k < this.array.length; k++) {
            if (this.array[k] != null) {
                result[j] = (byte) k;
                j++;
            }
        }
        return result;
    }
}